package com.cpc.mback.controller;


import com.cpc.mback.RespEntity;
import com.cpc.mback.entity.Content;
import com.cpc.mback.entity.Record;
import com.cpc.mback.service.MainService;
import com.cpc.mback.service.RecordService;
import com.cpc.mback.service.UploadService;
import com.cpc.mback.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

import static com.cpc.mback.controller.MainController.multipartFileToFile;

@RestController
@Api(value = "社区up接口", tags = "社区接口", description = "没有description")
@RequestMapping("/api/community-up")
public class CommunityUpController {

    @Autowired
    MainService mainService;

    @Autowired
    UploadService uploadService;
    @Autowired
    RecordService recordService;
    @Autowired
    UserService userService;

    @ApiOperation(value = "8.上传", notes = "上传文件获取 url \n" +
            " ")
    @PostMapping("/upload")
    public ResponseEntity upload(@RequestParam(value = "file") MultipartFile file ) throws Exception {

        String fileName = file.getOriginalFilename();
        File file2 = multipartFileToFile(file);
        if (file2.length()>104857600){
            return ResponseEntity.status(400).body(RespEntity.code(200).message("文件过大，无法上传").body("文件超过100M").build());
        }
        String resultUrl = uploadService.uploadOSS(file2);
        return ResponseEntity.status(200).body(RespEntity.code(200).message("上传成功:"+resultUrl).body(resultUrl).build());
    }
    @ApiOperation(value = "9.上传笔记或问题", notes = " \n" +
            " ")
    @PostMapping("/post-content")
    public ResponseEntity postContent(@RequestBody Payload payload) {

//        if(payload.type.equals("8")){
//            Content content = mainService.createContent(payload.title,Long.valueOf(payload.id),payload.content,Integer.parseInt(payload.type));
//
//        }

        Content content = mainService.createContent(payload.userName,payload.userAvatar,payload.title,payload.openId,
                payload.content,Integer.parseInt(payload.type),
                payload.time,payload.amount,payload.sort,payload.sort2,payload.sort3
                ,payload.img1,payload.img2,payload.img3
                ,payload.img4,payload.img5,payload.img6
                ,payload.img7,payload.img8,payload.img9
                ,payload.vid);
        return ResponseEntity.status(200).body(RespEntity.code(200).body(content).build());

    }


    @ApiOperation(value = "通用上传记录", notes = "参数只需要payload 里边的openId,id(content.id),content,type\n  sub 字段为判断是否为record的子record,如评论的评论，含义为子‘度数’，目前只支持1，默认为0" +
            "0 代表普通记录,1为评论或者回答，2为关注，3为收藏，4为点赞 ")
    @PostMapping("/post-record")
    public ResponseEntity postRecord(@RequestBody Payload payload) {
        if (payload.sub.equals(1)){
            Record record=recordService.createRecord(payload.openId,Long.valueOf(payload.id),payload.content,Integer.parseInt(payload.type),true,payload.contentUserId);
            return ResponseEntity.status(200).body(RespEntity.code(200).body(record).build());

        }
        Record record=recordService.createRecord(payload.openId,Long.valueOf(payload.id),payload.content,Integer.parseInt(payload.type),false,payload.contentUserId);


        return ResponseEntity.status(200).body(RespEntity.code(200).body(record).build());

    }


    @Data
    private static class Payload {
        private String userName;
        private String userAvatar;
        private String title;
        private String type;
        private String id;
        private String openId;
        private String content;
        private int intType;
        String sub;
        private String time;
        private String amount;
        private String sort;
        private String sort2;
        private String sort3;
        private String img1;
        private String img2;
        private String img3;
        private String img4;
        private String img5;
        private String img6;
        private String img7;
        private String img8;
        private String img9;
        private String vid;
        private String contentUserId;
    }

}
